<template>
  <BasicModal
    :footer="null"
    title="新增邮箱"
    v-bind="$attrs"
    :class="prefixCls"
    :bodyStyle="{ height: '170px' }"
    @register="register"
  >
    <div class="h-150px flex flex-col justify-center p-2">
      <BasicForm @register="registerForm" />
      <div :class="`${prefixCls}__footer`">
        <a-button type="primary" size="middle" @click="handleOk" :loading="loading">
          确定
        </a-button>
        <a-button type="primary" size="middle" class="ml-2" @click="handleCancel"> 取消 </a-button>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicModal, useModalInner } from '/@/components/Modal/index';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { getEmailCreate } from '/@/api/email/externalEmail';
  const { createMessage } = useMessage();

  export default defineComponent({
    name: 'SelTemplateModal',
    components: { BasicModal, BasicForm },
    emits: ['register', 'success'],
    setup(_, { emit }) {
      const { t } = useI18n();
      const { prefixCls } = useDesign('header-lock-modal');
      const loading = ref(false);
      const [register, { closeModal }] = useModalInner(async (data) => {
        console.log('data', data);
        resetFields();
      });

      const [registerForm, { validateFields, resetFields }] = useForm({
        showActionButtonGroup: false,
        labelWidth: 100,
        schemas: [
          {
            field: 'email',
            component: 'Input',
            label: '邮箱地址',
            colProps: {
              span: 23,
            },
            required: true,
            componentProps: {
              placeholder: '请输入邮箱地址',
            },
          },
          {
            field: 'emailPwd',
            component: 'InputPassword',
            label: '密码',
            colProps: {
              span: 23,
            },
            required: true,
            helpMessage: 'QQ邮箱请输入授权码',
            componentProps: {
              placeholder: 'QQ邮箱请输入授权码 ',
              autocomplete: 'new-password',
            },
          },
        ],
      });

      async function handleOk() {
        try {
          const values = (await validateFields()) as any;
          console.log('values', values);
          // 检查密码是否正确
          loading.value = true;
          const data = await getEmailCreate({ ...values });
          loading.value = false;
          console.log('data', data);
          if (data.res != 0) {
            createMessage.warn('操作失败');
            return;
          }

          closeModal();
          console.log('handleOk data', data);
          emit('success', data);
        } catch (error) {
          console.error(error);
          loading.value = false;
        }
      }

      async function handleCancel() {
        closeModal();
      }

      return {
        t,
        prefixCls,
        register,
        registerForm,
        handleOk,
        handleCancel,
      };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-header-input-pwd-modal';

  .@{prefix-cls} {
    &__entry {
      position: relative;
      //height: 240px;
      padding: 130px 30px 30px;
      border-radius: 10px;
    }

    &__header {
      position: absolute;
      top: 0;
      left: calc(50% - 45px);
      width: auto;
      text-align: center;

      &-img {
        width: 70px;
        border-radius: 50%;
      }

      &-name {
        margin-top: 5px;
      }
    }

    &__footer {
      text-align: center;
    }
  }
</style>
